# ----------------------------------------------
# Configuration variables taken from ./configure
# ----------------------------------------------
CPPFLAGS     = @CPPFLAGS@
CXX          = @CXX@
CXXFLAGS     = @CXXFLAGS@
DEFINES      = @DEFS@
LDFLAGS      = @LDFLAGS@
DWARFFLAGS      = @DWARFFLAGS@
ELFFLAGS      = @ELFFLAGS@
EXTRA_LIBS         = @LIBS@
DEMANGLER_LINK = @LIBERTYFLAGS@
prefix       = @prefix@
PLATFORM    ?= @build@
DYNINST_VER  = @DYNINST_VER@

INSTALL      = @INSTALL@

vpath % @srcdir@/src

# -------------------------------------------
# Begin Makefile based on variables set above
# -------------------------------------------
.PHONY: clean ready

SRCS         = parseThat.C \
               config.C \
               ipc.C \
               record.C \
               strlist.C \
               reglist.C \
               log.C \
               utils.C \
               sha1.C \
               dyninstCore.C

ifeq ($(DYNINST_VER), 5)
SRCS        += dyninstCompat.v5.C
endif

ifeq ($(DYNINST_VER), 4)
SRCS        += dyninstCompat.v4.C
endif

OBJS         = $(SRCS:%.C=%.o)

CXXFLAGS    += -Wall
LIBS        += -ldyninstAPI -lstackwalk -lpcontrol -lpatchAPI -lparseAPI -linstructionAPI -lsymtabAPI -lsymLite -ldynDwarf -ldynElf -lcommon -pthread

# The echo will remove the quotes
LIBS += $(shell echo $(DEMANGLER_LINK))

ifneq (x,x$(strip $(ELFFLAGS)))
LIBS += $(ELFFLAGS) 
endif
ifneq (x,x$(strip $(DWARFFLAGS)))
LIBS += $(DWARFFLAGS)
endif

ifeq (freebsd, $(findstring freebsd, $(PLATFORM)))
LIBS	    += -lpthread
endif

ifeq (ppc64_linux, $(findstring ppc64_linux, $(PLATFORM)))
CXXFLAGS	+= -m64
CFLAGS		+= -m64
endif

ifneq ($(DESTDIR),)
TARGETDIR = $(DESTDIR)/$(prefix)
else
TARGETDIR = $(prefix)
endif

LIBS += ${EXTRA_LIBS}

all: ready parseThat

install: $(TARGETDIR)/parseThat

$(TARGETDIR)/parseThat: parseThat
	@if [ $(TARGETDIR) != "." ]; then                           \
		echo "$(INSTALL) -d $(TARGETDIR)";                  \
		$(INSTALL) -d $(TARGETDIR);                         \
		echo "$(INSTALL) parseThat $(TARGETDIR)/parseThat"; \
		$(INSTALL) parseThat $(TARGETDIR)/parseThat;        \
	fi

clean:
	rm -f parseThat *.o *.so

distclean: clean
	rm Makefile config.log config.status

ready:
	@echo "Begin parseThat build for $(PLATFORM)"

parseThat: $(OBJS)
	$(CXX) $(CXXFLAGS) $^ -o $@ $(LDFLAGS) $(LIBS)

$(OBJS): %.o: %.C
	$(CXX) $(CXXFLAGS) $(CPPFLAGS) $(DEFINES) $^ -c -o $@

alarm: alarm.c liblib.so
	gcc -Wall -g -o alarm alarm.c liblib.so

liblib.so: lib.c
	gcc -Wall -g -shared -o liblib.so lib.c
